package com.qx.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.qx.po.Details;
import com.qx.po.Employees;
import com.qx.po.Goods;
import com.qx.po.Purchase;
import com.qx.service.IDetailsService;
import com.qx.service.IGoodsService;
import com.qx.service.IPurchaseService;
@Controller
public class GoodsController {
	@Autowired
    public IGoodsService igoodsservice;
	@Autowired
	public IPurchaseService ipurchaseservice;
	@Autowired
	public IDetailsService idetailsservice;
	
	 @RequestMapping("togoodsinformation")
	public String userinformation1(Model model, HttpSession session) throws Exception { 
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Goods> goodslist = igoodsservice.selectGoodsAll();
	   	 model.addAttribute("goodslist", goodslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/goodsinformation";
	    }
	 
	 @RequestMapping("topurchaseinformation")
	 public String topurchaseinformation(Model model, HttpSession session) throws Exception { 
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Purchase> purchaselist = ipurchaseservice.selectPurchaseAll();
	   	 model.addAttribute("purchaselist", purchaselist);
	     model.addAttribute("employees", employee);
	   	 return "/purchaseinformation";
	    }
	 
	 @RequestMapping("details/{pno}")
	 public String details(Model model, HttpSession session, @PathVariable("pno") String pno) throws Exception { 
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Details> detailslist = idetailsservice.selectDetailsBypno(pno);
	   	 model.addAttribute("detailslist", detailslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/details";
	    }
	 
	 @RequestMapping("togoods")
	 public String togoods(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Goods> goodslist = igoodsservice.selectGoodsAll();
		 model.addAttribute("goodslist", goodslist);
		 model.addAttribute("employees", employee);
		 return "/goodsinformation1";
		    }
	 @RequestMapping("deletegoods/{gno}")
	 public String deletegoods(Model model, HttpSession session, @PathVariable("gno") String gno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 igoodsservice.deleteGoodsBygno(gno);
		 List<Goods> goodslist = igoodsservice.selectGoodsAll();
		 model.addAttribute("goodslist", goodslist);
		 model.addAttribute("employees", employee);
		 return "/goodsinformation1";
	 		}
	 
	 @RequestMapping("toupdategoods/{gno}")
	 public String toupdategoods(Model model, HttpSession session, @PathVariable("gno") String gno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Goods goods = igoodsservice.selectGoodsBygno(gno);
		 model.addAttribute("goods", goods);
		 model.addAttribute("employees", employee);
		 return "/updategoods";
	 		}

	 @RequestMapping("togoodsinformation1/{gno}")
	 public String togoodsinformation1(Model model, @PathVariable("gno") String gno, @RequestParam("sno") String sno, @RequestParam("gname") String gname,
			 @RequestParam("gprice") String gprice, @RequestParam("gintroduce") String gintroduce, @RequestParam("gtips") String gtips,HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Goods goods = new Goods();
		 goods.setGno(gno);
		 goods.setSno(sno);
		 goods.setGname(gname);
		 goods.setGprice(gprice);
		 goods.setGintroduce(gintroduce);
		 goods.setGtips(gtips);
		 igoodsservice.updateGoodsBygno(goods);
		 List<Goods> goodslist = igoodsservice.selectGoodsAll();
		 model.addAttribute("goodslist", goodslist);
		 model.addAttribute("employees", employee);
		 return "/goodsinformation1";
	 		}
	 @RequestMapping("toaddgoods")
	 public String toaddgoods(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 model.addAttribute("employees", employee);
		 return "/addgoods";
	 		}

	 @RequestMapping("addgoods")
	 public String addgoods(@RequestParam("gno") String gno,  @RequestParam("sno") String sno, @RequestParam("gname") String gname,
			 @RequestParam("gprice") String gprice, @RequestParam("gintroduce") String gintroduce, @RequestParam("gtips") String gtips,
			 Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Goods goods = new Goods();
		 goods.setGno(gno);
		 goods.setSno(sno);
		 goods.setGname(gname);
		 goods.setGprice(gprice);
		 goods.setGintroduce(gintroduce);
		 goods.setGtips(gtips);
		 igoodsservice.insertGoods(goods);
		 List<Goods> goodslist = igoodsservice.selectGoodsAll();
		 model.addAttribute("goodslist", goodslist);
		 model.addAttribute("employees", employee);
		 return "/goodsinformation1";
	 		}
	 
	 @RequestMapping("topurchase")
	 public String topurchase(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Purchase> purchaselist = ipurchaseservice.selectPurchaseAll();
		 model.addAttribute("purchaselist", purchaselist);
		 model.addAttribute("employees", employee);
		 return "/purchaseinformation1";
		    }
	 @RequestMapping("deletepurchase/{pno}")
	 public String deletepurchase(Model model, HttpSession session, @PathVariable("pno") String pno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 ipurchaseservice.deletePurchaseBypno(pno);
		 List<Purchase> purchaselist = ipurchaseservice.selectPurchaseAll();
		 model.addAttribute("purchaselist", purchaselist);
		 model.addAttribute("employees", employee);
		 return "/purchaseinformation1";
	 		}
	 
	 @RequestMapping("toupdatepurchase/{pno}")
	 public String toupdatepurchase(Model model, HttpSession session, @PathVariable("pno") String pno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Purchase purchase = ipurchaseservice.selectPurchaseBypno(pno);
		 model.addAttribute("purchase", purchase);
		 model.addAttribute("employees", employee);
		 return "/updatepurchase";
	 		}

	 @RequestMapping("topurchaseinformation1/{pno}")
	 public String topurchaseinformation1(Model model, @PathVariable("pno") String pno, @RequestParam("eno") String eno, @RequestParam("pquantity") int pquantity,
			 @RequestParam("psunprice") String psunprice, @RequestParam("ptime") Date ptime, @RequestParam("ptips") String ptips, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Purchase purchase = new Purchase();
		 purchase.setPno(pno);
		 purchase.setEno(eno);
		 purchase.setPquantity(pquantity);
		 purchase.setPsunprice(psunprice);
		 purchase.setPtime(ptime);
		 purchase.setPtips(ptips);
		 ipurchaseservice.updatePurchaseBypno(purchase);
		 List<Purchase> purchaselist = ipurchaseservice.selectPurchaseAll();
		 model.addAttribute("purchaselist", purchaselist);
		 model.addAttribute("employees", employee);
		 return "/purchaseinformation1";
	 		}
	 @RequestMapping("toaddpurchase")
	 public String toaddpurchase(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 model.addAttribute("employees", employee);
		 return "/addpurchase";
	 		}
	 
	 @RequestMapping("addpurchase")
	 public String addpurchase(@RequestParam("pno") String pno,  @RequestParam("eno") String eno, @RequestParam("pquantity") int pquantity,
			 @RequestParam("psunprice") String psunprice, @RequestParam("ptime") Date ptime, @RequestParam("ptips") String ptips,
			 Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Purchase purchase = new Purchase();
		 purchase.setPno(pno);
		 purchase.setEno(eno);
		 purchase.setPquantity(pquantity);
		 purchase.setPsunprice(psunprice);
		 purchase.setPtime(ptime);
		 purchase.setPtips(ptips);
		 ipurchaseservice.insertPurchase(purchase);
		 List<Purchase> purchaselist = ipurchaseservice.selectPurchaseAll();
		 model.addAttribute("purchaselist", purchaselist);
		 model.addAttribute("employees", employee);
		 return "/purchaseinformation1";
	 		}
	 
	 @RequestMapping("details1/{pno}")
	 public String details1(Model model, HttpSession session, @PathVariable("pno") String pno) throws Exception { 
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Details> detailslist = idetailsservice.selectDetailsBypno(pno);
		 Purchase purchase1 = ipurchaseservice.selectPurchaseBypno(pno);
		 session.setAttribute("purchase", purchase1);
	   	 model.addAttribute("detailslist", detailslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/details1";
	    }
	 
	 @RequestMapping("deletedetails/{dno}")
	 public String deletedetails(Model model, HttpSession session, @PathVariable("dno") String dno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 idetailsservice.deleteDetailsBydno(dno);
		 Purchase purchase = (Purchase)session.getAttribute("purchase");
		 String pno = purchase.getPno();
		 List<Details> detailslist = idetailsservice.selectDetailsBypno(pno);
	   	 model.addAttribute("detailslist", detailslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/details1";
	 		}
	 
	 @RequestMapping("toupdatedetails/{dno}")
	 public String toupdatedetails(Model model, HttpSession session, @PathVariable("dno") String dno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Details details = idetailsservice.selectDetailsBydno(dno);
		 model.addAttribute("details", details);
		 model.addAttribute("employees", employee);
		 return "/updatedetails";
	 		}
	//(dno,pno,gno,dquantity,dprice,dsumprice,dtips)
	 @RequestMapping("todetailsinformation1/{dno}")
	 public String todetailsinformation1(Model model, @PathVariable("dno") String dno, @RequestParam("dno1") String dno1, @RequestParam("pno") String pno, @RequestParam("gno") String gno,
			 @RequestParam("dquantity") int dquantity, @RequestParam("dprice") String dprice, @RequestParam("dsumprice") String dsumprice,
			 @RequestParam("dtips") String dtips,HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Details details = new Details();
		 details.setDno(dno1);
		 details.setPno(pno);
		 details.setGno(gno);
		 details.setDquantity(dquantity);
		 details.setDprice(dprice);
		 details.setDsumprice(dsumprice);
		 details.setDtips(dtips);
		 idetailsservice.updateDetailsBydno(details);
		 List<Details> detailslist = idetailsservice.selectDetailsBypno(pno);
	   	 model.addAttribute("detailslist", detailslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/details1";
	 		}
	 @RequestMapping("toadddetails")
	 public String toadddetails(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 model.addAttribute("employees", employee);
		 return "/adddetails";
	 		}
	 
	 @RequestMapping("adddetails")
	 public String adddetails(@RequestParam("dno") String dno, @RequestParam("pno") String pno, @RequestParam("gno") String gno,
			 @RequestParam("dquantity") int dquantity, @RequestParam("dprice") String dprice, @RequestParam("dsumprice") String dsumprice,
			 @RequestParam("dtips") String dtips,
			 Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Details details = new Details();
		 details.setDno(dno);
		 details.setPno(pno);
		 details.setGno(gno);
		 details.setDquantity(dquantity);
		 details.setDprice(dprice);
		 details.setDsumprice(dsumprice);
		 details.setDtips(dtips);
		 idetailsservice.insertDetails(details);
		 List<Details> detailslist = idetailsservice.selectDetailsBypno(pno);
	   	 model.addAttribute("detailslist", detailslist);
	   	 model.addAttribute("employees", employee);
	   	 return "/details1";
	 		}

}
